//
//  PersonCenterViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/13.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "PersonCenterViewController.h"

#import "PersonCenterHeaderView.h"

#import "PersonCenterInviteView.h"

#import "PersonCenterOrdersView.h"

#import "LoginViewController.h"

#import "MyOrdersViewController.h"

#import "PersonReplenishingView.h"

@interface PersonCenterViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) PersonCenterHeaderView *topHeaderView;//顶部view

@property (nonatomic,strong) PersonCenterInviteView *inviteView;//邀请view

@property (nonatomic,strong) UIImageView *inviteBgImgV;//邀请背景图

@property (nonatomic,strong) UIImageView *inviteImgV;//邀请图片

@property (nonatomic,strong) PersonCenterOrdersView *bottomOrdersView;//底部订单view

@property (nonatomic,strong) PersonReplenishingView *replenishingView;//更多菜单view

@property (nonatomic,strong) UIImageView *downLoadImgV;//分享 下载app

@property (nonatomic,strong) UIButton *loginOutBtn;//退出登录

@property (nonatomic,assign) BOOL charge;//是否显示更多 菜单

@end

@implementation PersonCenterViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.charge = YES;
    [self initUI];
}
#pragma mark - data
-(void)getData{
    self.loginOutBtn.hidden = ![UserInfomationManager shareManager].loginStatus;
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        return;
    }
    WEAKSELF(self);
    [NetWorkingPerson getUserHomeWithSuccessBlock:^(id  _Nonnull responseObj) {
        NSDictionary *data = responseObj[@"data"];
        NSDictionary *userDict = data[@"user"];
        NSDictionary *inviteDict = data[@"total"];
        weakSelf.topHeaderView.userInfoDict = userDict;
        weakSelf.inviteView.inviteInfoDict = inviteDict;
        [UserInfomationManager updateUserInfo:userDict];
        weakSelf.charge = [data[@"charge"] boolValue];
        [weakSelf reloadReplenishingView];
    }];
}
-(void)reloadReplenishingView{
    if (self.charge == NO) {
        self.replenishingView.hidden = YES;
        self.loginOutBtn.top = self.bottomOrdersView.bottom + 25;
    }else{
        self.replenishingView.hidden = NO;
        self.loginOutBtn.top = self.replenishingView.bottom + 25;
    }
    self.scrollView.contentSize = CGSizeMake(ScreenWidth, self.loginOutBtn.bottom+30);
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.topHeaderView];
    [self.scrollView addSubview:self.inviteView];
    [self.scrollView addSubview:self.inviteBgImgV];
    [self.scrollView addSubview:self.inviteImgV];
    [self.scrollView addSubview:self.bottomOrdersView];
    [self.scrollView addSubview:self.replenishingView];
    [self.scrollView addSubview:self.downLoadImgV];
    [self.scrollView addSubview:self.loginOutBtn];
    
    self.scrollView.contentSize = CGSizeMake(ScreenWidth, self.loginOutBtn.bottom+30);
    
    [self viewBlocks];
}
//view中的点击事件block
-(void)viewBlocks{
    WEAKSELF(self);
    //底部订单view blocks
    [self.bottomOrdersView setTouchOrdersBlock:^(NSInteger index) {
        if ([UserInfomationManager shareManager].loginStatus == NO) {
            [weakSelf loginClick];
            return;
        }
        MyOrdersViewController *orderVC = [[MyOrdersViewController alloc] init];
        orderVC.sourceType = index + 1;
        [weakSelf.navigationController pushViewController:orderVC animated:YES];
    }];
    [self.bottomOrdersView setTouchAllOrdersBlock:^{
        if ([UserInfomationManager shareManager].loginStatus == NO) {
            [weakSelf loginClick];
            return;
        }
        MyOrdersViewController *orderVC = [[MyOrdersViewController alloc] init];
        orderVC.sourceType = 0;
        [weakSelf.navigationController pushViewController:orderVC animated:YES];
    }];
    
    [self.topHeaderView setUpdateUserInfoBlock:^{
        [weakSelf getData];
    }];
    
    [self.replenishingView setTouchBlock:^(NSInteger index) {
        if ([UserInfomationManager shareManager].loginStatus == NO) {
            [weakSelf loginClick];
            return;
        }
        [weakSelf moreMenuTouch:index];
    }];
}
#pragma mark - lazy
-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -kHeight_StatusBar-5, ScreenWidth, ScreenHeight-kHeight_TabBar+kHeight_StatusBar+5)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.backgroundColor = Color_Clear;
        _scrollView.bounces = NO;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}
//顶部header
-(PersonCenterHeaderView *)topHeaderView{
    if (!_topHeaderView) {
        _topHeaderView = [[PersonCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth*0.75)];
    }
    return _topHeaderView;
}
//邀请view
- (PersonCenterInviteView *)inviteView{
    if (!_inviteView) {
        _inviteView = [[PersonCenterInviteView alloc] initWithFrame:CGRectMake(10, self.topHeaderView.bottom, ScreenWidth-20, (ScreenWidth-20)/3+20)];
    }
    return _inviteView;
}
//邀请好友imgv
- (UIImageView *)inviteBgImgV{
    if (!_inviteBgImgV) {
        _inviteBgImgV = [[UIImageView alloc] initWithFrame:CGRectMake(12, self.inviteView.bottom+30, ScreenWidth-24, (ScreenWidth-24)/7)];
        _inviteBgImgV.image = LoadImage(@"指南商城_邀请好友背景");
        
        _inviteBgImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareRegisterClick:)];
        ges.delegate = self;
        [_inviteBgImgV addGestureRecognizer:ges];
    }
    return _inviteBgImgV;
}
- (UIImageView *)inviteImgV{
    if (!_inviteImgV) {
        _inviteImgV = [[UIImageView alloc] initWithFrame:CGRectMake(self.inviteBgImgV.right-((self.inviteBgImgV.height+20)*6/7)-10, self.inviteBgImgV.top-15, (self.inviteBgImgV.height+20)*6/7, self.inviteBgImgV.height+20)];
        _inviteImgV.image = LoadImage(@"指南商城_邀请好友");
    }
    return _inviteImgV;
}
- (UIImageView *)downLoadImgV{
    if (!_downLoadImgV) {
        _downLoadImgV = [[UIImageView alloc] initWithFrame:CGRectMake(12, self.inviteImgV.bottom + 10, ScreenWidth-24, (ScreenWidth-24)/5)];
        _downLoadImgV.image = LoadImage(@"分享_下载");
        
        _downLoadImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToDownloadClick:)];
        ges.delegate = self;
        [_downLoadImgV addGestureRecognizer:ges];
    }
    return _downLoadImgV;
}

- (PersonCenterOrdersView *)bottomOrdersView{
    if (!_bottomOrdersView) {
        _bottomOrdersView = [[PersonCenterOrdersView alloc] initWithFrame:CGRectMake(0, self.downLoadImgV.bottom+22, ScreenWidth, 140)];
    }
    return _bottomOrdersView;
}
- (PersonReplenishingView *)replenishingView{
    if (!_replenishingView) {
        _replenishingView = [[PersonReplenishingView alloc] initWithFrame:CGRectMake(0, self.bottomOrdersView.bottom+12, ScreenWidth, 140)];
    }
    return _replenishingView;
}

- (UIButton *)loginOutBtn{
    if (!_loginOutBtn) {
        _loginOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginOutBtn.frame = CGRectMake(50, self.replenishingView.bottom + 25, ScreenWidth-100, 40);
        _loginOutBtn.clipsToBounds = YES;
        _loginOutBtn.layer.cornerRadius = 20;
        _loginOutBtn.layer.borderWidth = 1;
        _loginOutBtn.layer.borderColor = RGB(37, 130, 255).CGColor;
        [_loginOutBtn setTitle:@"退出登录" forState:normal];
        [_loginOutBtn setTitleColor:RGB(37, 130, 255) forState:normal];
        [_loginOutBtn.titleLabel setFont:FontSet(17)];
        [_loginOutBtn addTarget:self action:@selector(loginOutClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginOutBtn;
}
#pragma mark - SEL
//退出登录
-(void)loginOutClick{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要退出登录吗？" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [UserInfomationManager loginOut];
        [[XNProgressHUD shared] showWithTitle:@"退出登录"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self loginClick];
        });
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
//去登陆
-(void)loginClick{
    [UserInfomationManager loginOut];
    LoginViewController *vc = [[LoginViewController alloc] init];
    vc.modalPresentationStyle = 0;
    [self presentViewController:vc animated:YES completion:nil];
}
//分享
-(void)shareRegisterClick:(UIGestureRecognizer *)ges{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [Configuration loginClick];
        return;
    }else{
        if (![WXApi isWXAppInstalled]) {
            [[XNProgressHUD shared] showWithTitle:@"您还未安装微信,请移步App Store去下载微信客户端"];
        }else {
            [self shareToWXMiniWithType:0];
        }
    }
}
-(void)shareToDownloadClick:(UIGestureRecognizer *)ges{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        [Configuration loginClick];
        return;
    }else{
        if (![WXApi isWXAppInstalled]) {
            [[XNProgressHUD shared] showWithTitle:@"您还未安装微信,请移步App Store去下载微信客户端"];
        }else {
            [self shareToWXMiniWithType:1];
        }
    }
}
//分享到微信小程序
-(void)shareToWXMiniWithType:(NSInteger)type{
    NSString *path;
    if (type == 0) {
        path = [NSString stringWithFormat:@"pages/my/my?inviterId=%ld",(long)[UserInfomationManager shareManager].userId];
    }else{
        path = [NSString stringWithFormat:@"pages/my/my?func=app&inviterId=%ld",(long)[UserInfomationManager shareManager].userId];
    }
    
    WXMiniProgramObject *object = [WXMiniProgramObject object];
    object.webpageUrl = @"www.baidu.com";
    object.userName = @"gh_56dec2c0ddb1";
    object.path = path;
    
    object.hdImageData = UIImageJPEGRepresentation([UIImage imageNamed:@"分享_邀请好友"], 0.3);
    object.withShareTicket = YES;
    object.miniProgramType = 0;
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"指南在首";
    message.description = @"邀请好友得TB";
    message.thumbData = UIImageJPEGRepresentation([UIImage imageNamed:@"分享_邀请好友"], 0.3);  //兼容旧版本节点的图片，小于32KB，新版本优先
    message.mediaObject = object;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;  //目前只支持会话
    [WXApi sendReq:req completion:nil];
}

//更多view 条目点击事件
-(void)moreMenuTouch:(NSInteger)index{
    if (index == 0) {
        [JumpManager jumpToReplenishingVC];
    }
}



@end




